<?php
namespace Api\Controller;
use Think\Controller;
class AccountController extends CommonController {
    function index(){
        if(IS_POST){
            $info['year']=(I('post.year'))?I('post.year'):(date('Y'));
            $info['month']=(I('post.month'))?I('post.month'):(date('m'));
            //该月的开始时间戳和结束时间戳
            $mydate['beginThismonth']=strtotime($info['year'].'-'. $info['month']);
            $day=date('t',$mydate['beginThismonth']);
            $mydate['endThismonth']=$mydate['beginThismonth']+ 86400*$day  - 1;
            //按照日期的
            $sql='SELECT *,FROM_UNIXTIME( create_time,\'%d\') as mydate FROM pq_account '.'where create_time >'.$mydate['beginThismonth'].' and create_time <'.$mydate['endThismonth'].' order by mydate desc,create_time desc';
            $result = M('account')->query($sql);
            $mydata=array();
            $mydaydata=array();
            $account['in']=0;
            $account['out']=0;
            foreach ($result as $value){
                if ($value['bigtype'] == 1){//收入
                    $mydata[$value['mydate']][]=$value;
                    $mydaydata[$value['mydate']]['day']=date('m月d日',$value['create_time']);
                    $mydaydata[$value['mydate']]['in'] += $value['money'];
                    $account['in'] += $value['money'];
                    $value['tag']='+';
                }
                else if ($value['bigtype'] == 2){//支出
                    $mydata[$value['mydate']][]=$value;
                    $mydaydata[$value['mydate']]['day']=date('m月d日',$value['create_time']);
                    $mydaydata[$value['mydate']]['out'] += $value['money'];
                    $account['out'] += $value['money'];
                    $value['tag']='-';
                }
            }
            $account['get'] = $account['in'] -$account['out'];
            $account['month'] = $info['month'];
            $account_data['mydata'] = $mydata;
            $account_data['mydaydata'] = $mydaydata;
            $account_data['account'] = $account;
            echo lbjson(0,'数据请求成功',$account_data);

        }else
            echo lbjson(1,'请求方式错误',[]);
    }
   public function addaccount(){
        if(IS_POST){
            $data = I('post.');
            $data['create_time'] = time();
            $tag = M('account')->add($data);
            if($tag)
                echo lbjson(0,'添加成功',['id'=>$tag]);
            else
                echo lbjson(1,'添加失败',[]);
        }else
            echo lbjson(1,'请求方式错误',[]);
   }
}